/**
 * SQL操作类型枚举
 * 
 * - SELECT: 查询操作
 * - INSERT: 插入操作
 * - UPDATE: 更新操作
 * - DELETE: 删除操作
 */
export type SqlOperationType = 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE';

/**
 * SQL操作类型常量
 */
export const SQL_OPERATION_TYPE = {
  SELECT: 'SELECT' as SqlOperationType,
  INSERT: 'INSERT' as SqlOperationType,
  UPDATE: 'UPDATE' as SqlOperationType,
  DELETE: 'DELETE' as SqlOperationType
};

/**
 * 条件操作符枚举
 * 
 * - EQ: 等于(=)
 * - NE: 不等于(<>)
 * - GT: 大于(>)
 * - GTE: 大于等于(>=)
 * - LT: 小于(<)
 * - LTE: 小于等于(<=)
 * - LIKE: 模糊匹配(LIKE)
 * - NOT_LIKE: 模糊匹配取反(NOT LIKE)
 * - IN: 包含(IN)
 * - NOT_IN: 不包含(NOT IN)
 * - IS_NULL: 为空(IS NULL)
 * - IS_NOT_NULL: 不为空(IS NOT NULL)
 * - BETWEEN: 区间(BETWEEN)
 * - NOT_BETWEEN: 不在区间(NOT BETWEEN)
 * - EXISTS: 存在子查询(EXISTS)
 * - NOT_EXISTS: 不存在子查询(NOT EXISTS)
 * - RAW: 原始SQL片段
 */
export type ConditionOperator = 
  | '=' 
  | '<>' 
  | '>' 
  | '>=' 
  | '<' 
  | '<=' 
  | 'LIKE' 
  | 'NOT LIKE' 
  | 'IN' 
  | 'NOT IN' 
  | 'IS NULL' 
  | 'IS NOT NULL' 
  | 'BETWEEN' 
  | 'NOT BETWEEN' 
  | 'EXISTS' 
  | 'NOT EXISTS'
  | 'RAW';

/**
 * 条件操作符常量
 */
export const CONDITION_OPERATOR = {
  EQ: '=' as ConditionOperator,
  NE: '<>' as ConditionOperator,
  GT: '>' as ConditionOperator,
  GTE: '>=' as ConditionOperator,
  LT: '<' as ConditionOperator,
  LTE: '<=' as ConditionOperator,
  LIKE: 'LIKE' as ConditionOperator,
  NOT_LIKE: 'NOT LIKE' as ConditionOperator,
  IN: 'IN' as ConditionOperator,
  NOT_IN: 'NOT IN' as ConditionOperator,
  IS_NULL: 'IS NULL' as ConditionOperator,
  IS_NOT_NULL: 'IS NOT NULL' as ConditionOperator,
  BETWEEN: 'BETWEEN' as ConditionOperator,
  NOT_BETWEEN: 'NOT BETWEEN' as ConditionOperator,
  EXISTS: 'EXISTS' as ConditionOperator,
  NOT_EXISTS: 'NOT EXISTS' as ConditionOperator,
  RAW: 'RAW' as ConditionOperator
};

/**
 * 逻辑操作符枚举
 * 
 * - AND: 与
 * - OR: 或
 */
export type LogicalOperator = 'AND' | 'OR';

/**
 * 逻辑操作符常量
 */
export const LOGICAL_OPERATOR = {
  AND: 'AND' as LogicalOperator,
  OR: 'OR' as LogicalOperator
};

/**
 * 排序方向枚举
 * 
 * - ASC: 升序
 * - DESC: 降序
 */
export type OrderDirection = 'ASC' | 'DESC';

/**
 * 排序方向常量
 */
export const ORDER_DIRECTION = {
  ASC: 'ASC' as OrderDirection,
  DESC: 'DESC' as OrderDirection
};

/**
 * 联接类型枚举
 * 
 * - INNER: 内联接
 * - LEFT: 左外联接
 * SQLite不支持RIGHT JOIN和FULL JOIN
 */
export type JoinType = 'INNER JOIN' | 'LEFT JOIN';

/**
 * 联接类型常量
 */
export const JOIN_TYPE = {
  INNER: 'INNER JOIN' as JoinType,
  LEFT: 'LEFT JOIN' as JoinType
};

/**
 * 实体类字段映射接口
 */
export interface EntityFieldMapping {
  /** 实体类中的属性名 */
  propertyName: string;
  /** 数据库表中的字段名 */
  columnName: string;
  /** 数据类型 */
  dataType?: string;
  /** 是否为主键 */
  isPrimaryKey?: boolean;
  /** 是否自动递增 */
  isAutoIncrement?: boolean;
  /** 是否允许为空 */
  isNullable?: boolean;
  /** 默认值 */
  defaultValue?: any;
}

/**
 * 表实体映射接口
 */
export interface TableEntityMapping {
  /** 表名 */
  tableName: string;
  /** 主键字段名称 */
  primaryKey?: string;
  /** 字段映射 */
  fields: Record<string, EntityFieldMapping>;
}

/**
 * SQL参数接口
 */
export interface SqlParameter {
  /** 参数名称 */
  name: string;
  /** 参数值 */
  value: any;
}

/**
 * SQL条件接口
 */
export interface Condition {
  /** 字段名 */
  field: string;
  /** 操作符 */
  operator: ConditionOperator;
  /** 值 */
  value?: any;
  /** 逻辑操作符 */
  logicalOperator?: LogicalOperator;
  /** 参数名称（用于参数化查询） */
  parameterName?: string | string[];
  /** 子条件（用于嵌套条件） */
  conditions?: Condition[];
}

/**
 * SQL排序接口
 */
export interface OrderBy {
  /** 排序字段 */
  field: string;
  /** 排序方向 */
  direction: OrderDirection;
}

/**
 * 联接条件接口
 */
export interface JoinCondition {
  /** 联接类型 */
  type: JoinType;
  /** 联接表名 */
  table: string;
  /** 联接表别名 */
  alias?: string;
  /** 联接条件 */
  on: Condition | Condition[];
  /** 表实体映射 */
  entityMapping?: TableEntityMapping;
}

/**
 * SQL构建结果接口
 */
export interface SqlBuildResult {
  /** SQL语句 */
  sql: string;
  /** 参数对象 */
  params: Record<string, any>;
}

/**
 * 表构建结果接口
 * 扩展了SqlBuildResult，增加了索引SQL语句数组
 */
export interface TableBuildResult extends SqlBuildResult {
  /** 索引SQL语句数组 */
  indexSqls: string[];
}

/**
 * 实体类型
 * 泛型T代表实体对象的类型
 */
export type Entity<T = any> = {
  [P in keyof T]: T[P];
};

/**
 * 字段选择类型
 * T代表实体类型，K代表字段名称
 */
export type FieldSelection<T> = Array<keyof T | string> | '*';

/**
 * 查询结果类型
 * 为查询结果提供类型安全
 */
export type QueryResult<T> = T[];

/**
 * 单行查询结果类型
 */
export type SingleResult<T> = T | undefined;

/**
 * 字段更新对象类型
 */
export type UpdateFields<T> = Partial<{
  [P in keyof T]: T[P];
}>;

/**
 * 字段筛选器类型
 * 用于从实体类型中选择部分字段
 */
export type Pick<T, K extends keyof T> = {
  [P in K]: T[P];
};

/**
 * 条件参数类型
 * 使条件构建更加灵活
 */
export type ConditionParam<T> = 
  | { field: keyof T; operator: ConditionOperator; value: any }
  | { [K in keyof T]?: any };

/**
 * SQL扩展配置接口
 * 用于为SQL构建器提供扩展点
 */
export interface SqlBuilderExtensions {
  /** 自定义字段处理器 */
  fieldProcessors?: Array<(field: string, context: any) => string>;
  /** 自定义值处理器 */
  valueProcessors?: Array<(value: any, context: any) => any>;
  /** 自定义SQL钩子 */
  sqlHooks?: {
    beforeBuild?: (context: any) => void;
    afterBuild?: (result: SqlBuildResult, context: any) => SqlBuildResult;
  };
}

/**
 * SQL执行器接口
 * 用于执行SQL语句并处理结果
 */
export interface SqlExecutor {
  /**
   * 执行SQL查询语句并返回多行结果
   * @param sql SQL语句
   * @param params 参数对象
   */
  query<T = any>(sql: string, params?: Record<string, any>): T[];
  
  /**
   * 执行SQL查询语句并返回单行结果
   * @param sql SQL语句
   * @param params 参数对象
   */
  queryOne<T = any>(sql: string, params?: Record<string, any>): T | undefined;
  
  /**
   * 执行SQL语句（插入、更新、删除等）
   * @param sql SQL语句
   * @param params 参数对象
   */
  execute(sql: string, params?: Record<string, any>): SqlExecuteResult;
  
  /**
   * 执行事务
   * @param callback 事务回调函数
   */
  transaction<T>(callback: (executor: SqlExecutor) => T): T;
}

/**
 * SQL执行结果接口
 */
export interface SqlExecuteResult {
  /** 影响的行数 */
  changes: number;
  /** 最后插入的行ID */
  lastInsertRowid?: number | bigint;
}

/**
 * 表列定义
 */
export interface TableColumn {
  name: string;
  type: string;
  primaryKey?: boolean;
  autoIncrement?: boolean;
  notNull?: boolean;
  unique?: boolean;
  defaultValue?: any;
  check?: string;
  comment?: string;
  references?: {
    table: string;
    column: string;
    onUpdate?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT' | 'NO ACTION';
    onDelete?: 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT' | 'NO ACTION';
  }
}

/**
 * 时间戳选项
 */
export interface TimestampsOptions {
  /**
   * 创建时间列名，默认为created_at
   */
  createdAt?: string;
  
  /**
   * 更新时间列名，默认为updated_at
   */
  updatedAt?: string;
  
  /**
   * 是否使用毫秒级时间戳，默认为false
   * 如果为true，将使用strftime('%s') || '000'作为默认值
   * 如果为false，将使用CURRENT_TIMESTAMP作为默认值
   */
  useMilliseconds?: boolean;
  
  /**
   * 是否设置为非空，默认为true
   */
  notNull?: boolean;
}

/**
 * 软删除选项
 */
export interface SoftDeleteOptions {
  /**
   * 删除时间列名，默认为deleted_at
   */
  deletedAt?: string;
  
  /**
   * 是否使用毫秒级时间戳，默认为false
   */
  useMilliseconds?: boolean;
  
  /**
   * 是否设置为非空，默认为false
   */
  notNull?: boolean;
} 